<?php
/**
 * Description of User_OrderController
 * implement 5 action
 * @author: Beverley
 */

class User_OrderController extends Zend_Controller_Action {
    // view order
    function indexAction() {
        $cate = new Category();
        $this->view->cateList = $cate->fetchAll();

        $orderDetail = new OrderDetailDAO();
        $productSession = new Zend_Session_Namespace('product');
        if (!isset($productSession->product) || $orderDetail->checkCartIsEmpty($productSession->product)) {
            $this->view->status = "Your cart is empty.";
        }
    }
    
    // view order details
    function viewAction() {
        $cate = new Category();
        $this->view->cateList = $cate->fetchAll();
        
        $userSession = new Zend_Session_Namespace('user');
        $user_id = $userSession->user_id;

        $order = new OrderDAO();
        $orderList = $order->getOrderListByUserID($user_id);

        if (count($orderList)) {
            $paginator = Zend_Paginator::factory($orderList);
            $page = $this->_getParam('page',1);
            $paginator->setItemCountPerPage(2);
            $paginator->setCurrentPageNumber($page);
            $this->view->paginator = $paginator;
        }
        else {
            $this->view->status = "your order is empty.";
        }
    }

   
    // checkout a cart
    function orderAction() {
        $cate = new Category();
        $this->view->cateList = $cate->fetchAll();

        $name = $this->_request->getParam('name');
        $address = $this->_request->getParam('address');
        $phone = $this->_request->getParam('phone');

        $string_validate = new Zend_Validate_StringLength(3, 15);
        if (!$string_validate->isValid($name)) {
            $mess['name'] = "the name input is too short or too long";
        }

        $string_validate = new Zend_Validate_StringLength(3, 30);
        if (!$string_validate->isValid($address)) {
                $mess['address'] = "the address input is too short or too long";
        }

        $digit_validate = new Zend_Validate_Digits();
        if (!$digit_validate->isValid($phone)) {
            $mess['digit'] = "receiver phone must contain only digit character.";
        } else {
            $digit_validate = new Zend_Validate_StringLength(6, 16);
            if (!$digit_validate->isValid($phone)) {
                $mess['greater'] = "receiver phone is too short or too long.";
            }
        }
        $receive_date = $this->getRequest()->getPost('date');
      
        if (!isset($mess)) {
            $date = getdate();
            $order_date = $date['year']."/".$date['mon']."/".$date['mday'];

            $productSession = new Zend_Session_Namespace('product');
            $total = $productSession->total;

            $userSession = new Zend_Session_Namespace('user');
            $user_id = $userSession->user_id;
                
            $dataOrder = array(
                    'user_id'=>$user_id,
                    'order_date'=>$order_date,
                    'receive_date'=>$receive_date,
                    'receiver_name'=>$name,
                    'receiver_address'=>$address,
                    'receiver_phone'=>$phone,
                    'total'=>$total,
                    'status'=>1 );

            // add order into order table
            $order = new OrderDAO();
            $order_id = $order->addOrder($dataOrder);

            // add order into order_detail table
            $order_detail = new OrderDetailDAO();           
            $array = $productSession->product;
            $product = new Product();
            foreach($array as $id=>$quantity) {
                if ($quantity > 0) {
                    $row = $product->fetchRow("id = '$id'");
                    $price = $quantity*$row->price;

                    $dataOrderDetail = array(
                            'order_id'=>$order_id,
                            'product_id'=>$id,
                            'quantity'=>$quantity,
                            'price'=>$price,
                            'status'=> 1 );
                    $order_detail->addOrderDetail($dataOrderDetail);
                }
            }
            // after order cart, set cart is empty
            $productSession->__unset('product');
        }
        if (isset($mess)) {
            $this->view->messages = $mess;
        }
    }
}
?>
